import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import {
  NestExpressApplication,
  ExpressAdapter,
} from '@nestjs/platform-express';
import { initSwagger } from './document/swagger-config';
import { join } from 'path';
import { ValidationPipe } from '@nestjs/common';
import * as session from "express-session";

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(
    AppModule,
    new ExpressAdapter(),
  );
  // docs文档url：通过http://localhost:3000/docs
  initSwagger(app);
  // 配置静态资源目录
  app.useStaticAssets(join(__dirname, '..', '/public/image'));
  app.useStaticAssets(join(__dirname, '..', '/public/upload/user'));
  app.useStaticAssets(join(__dirname, '..', '/public/upload/group'));
  // 配置全局请求路径前缀
  app.setGlobalPrefix('/api/cms');
  // 允许跨域
  app.enableCors();
  // session
  app.use(
    session({
      secret: new Date().getTime().toString(),
      rolling: true,
      name: "captcha_sid",
      cookie: {
        maxAge: 1 * 10 * 60 * 1000, // 10分钟
      },
    })
  );
  // 添加全局管道
  app.useGlobalPipes(
    new ValidationPipe({
      // 去除在类上不存在的字段
      whitelist: true,
    }),
  );
  await app.listen(3000);
}
bootstrap();
